package com.mlink.ai.chat.ui.activity

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.text.method.LinkMovementMethod
import android.view.View
import androidx.core.content.ContextCompat
import com.mlink.ai.chat.assistant.robot.R
import com.mlink.ai.chat.constants.IS_CLICKED_GET_STARTED_BTN
import com.mlink.ai.chat.assistant.robot.databinding.ActivityStartBinding
import com.mlink.ai.chat.constants.PRIVACY_POLICY_URL
import com.mlink.ai.chat.constants.TERMS_OF_SERVICE_URL
import com.mlink.ai.chat.utils.AnalyticsEvents
import com.mlink.ai.chat.utils.ClickableColorText
import com.mlink.ai.chat.utils.ColorText
import com.mlink.ai.chat.utils.logEvent
import com.mlink.ai.chat.utils.openUrl
import com.tencent.mmkv.MMKV

class StartActivity : BaseActivity<ActivityStartBinding>() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        window.decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        window.statusBarColor = Color.TRANSPARENT

        AnalyticsEvents.AC_LAUNCH_PAGE_SHOW.logEvent()
    }

    override fun getViewBinding(): ActivityStartBinding {
        return ActivityStartBinding.inflate(layoutInflater)
    }

    override fun initViews() {

        ColorText(resources.getString(R.string.your_best_ai_assistant))
            .with(
                resources.getString(R.string.ai), ContextCompat.getColor(
                    this,
                    R.color.btn_green
                )
            ).build().run {
                binding.yourBestTv.text = this

            }

        val startPrivacyText = resources.getString(R.string.start_privacy)
        val termsOfServiceTxt = resources.getString(R.string.terms_of_service)
        val privacyPolicyTxt = resources.getString(R.string.privacy_policy)
        ClickableColorText(startPrivacyText).run {
            addClickableText(
                termsOfServiceTxt, ContextCompat.getColor(
                    this@StartActivity,
                    R.color.green_50
                )
            ) {
                openUrl(TERMS_OF_SERVICE_URL)
            }

            addClickableText(
                privacyPolicyTxt, ContextCompat.getColor(
                    this@StartActivity,
                    R.color.green_50
                )
            ) {
                openUrl(PRIVACY_POLICY_URL)
            }

            binding.startPrivacyTv.text = this
            binding.startPrivacyTv.movementMethod = LinkMovementMethod.getInstance()
        }

        binding.startBtn.setOnClickListener {
            AnalyticsEvents.AC_LAUNCH_PAGE_START_CLICK.logEvent()
            startActivity(Intent(this, LanguageActivity::class.java))
            MMKV.defaultMMKV().encode(IS_CLICKED_GET_STARTED_BTN, true)
            finish()
        }
    }

}